package com.langxin.site.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.langxin.site.dao.PrecautionsDao;
import com.langxin.site.model.PageInfo;
import com.langxin.site.model.PrecautionsInfo;
import com.langxin.site.model.vo.QueryVo;
import com.langxin.site.util.constant.SiteConstant;

/**
 * 注意事项/须带物品biz
 * @author guqj
 *
 */
@Service
public class PrecautionsBiz extends BaseBiz{

	@Autowired
	private PrecautionsDao  precautionsDao;
	
	/**
	 * 条件查询
	 * @param baseVo   参数
	 * @param pageSize 每页记录数
	 * @return
	 */
	public List<PrecautionsInfo> queryList(QueryVo queryVo, Integer pageSize, Integer type, String orderBy){
		if(queryVo == null){
			queryVo = new QueryVo();
		}
		if(pageSize != null){
			queryVo.setPageSize(pageSize);
		}
		if(type != null){
			queryVo.setType(type);
		}
		if(queryVo.getType() == null || (SiteConstant.NOTICE_TYPE_1.compareTo(queryVo.getType()) != 0 
				&& SiteConstant.NOTICE_TYPE_0.compareTo(queryVo.getType()) != 0)){
			queryVo.setType(SiteConstant.NOTICE_TYPE_1);
		}
		if(StringUtils.isNotBlank(orderBy)){
			queryVo.setOrderBy(orderBy);
		}
		return precautionsDao.query(queryVo);
	}
	
	/**
	 * 总数
	 * @param queryVo
	 * @return
	 */
	public Integer queryNum(QueryVo queryVo){
		return precautionsDao.queryNum(queryVo);
	}
	
	/**
	 * 分页
	 * @param queryVo
	 * @param pageInfo
	 */
	public void queryPage(QueryVo queryVo, PageInfo<PrecautionsInfo> pageInfo){
		pageInfo.setRecords(queryList(queryVo, 16, null, null));
		pageInfo.setTotalCount(queryNum(queryVo));
		pageInfo.fill(queryVo);
	}
	
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	public PrecautionsInfo queryById(Integer id){
		if (!isRead(SiteConstant.CookieKey.CAUTION.getValue(), id)) {
			precautionsDao.updateReadTime(id);
		}
		return precautionsDao.queryById(id);
	}
	
	/**
	 * 热榜
	 * @param pageSize
	 * @return
	 */
	public List<PrecautionsInfo> hot(Integer pageSize, Integer type){
		return queryList(null, pageSize, type,"T." + SiteConstant.ORDER_READ_TIME + " " + SiteConstant.DESC);
	}
}
